package com.njust.diary.Controlller;

import com.njust.diary.po.Health;
import com.njust.diary.po.NoteType;
import com.njust.diary.po.User;
import com.njust.diary.service.HealthService;
import com.njust.diary.service.UserService;
import com.njust.diary.vo.Result;
import org.apache.commons.io.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.CollationKey;
import java.util.List;

@WebServlet("/user")
@MultipartConfig
public class UserServlet extends HttpServlet {

private UserService userService =new UserService();
private HealthService healthService =new HealthService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {



        //判断用户行为
        String actionName =req.getParameter("actionName");
        if("login".equals(actionName)){
            userLogin(req,resp);

        }else if("logout".equals(actionName)){
            userLogout(req,resp);
        }else if("userCenter".equals(actionName)){
            userCenter(req,resp);
        }else if("userHead".equals(actionName)){
            userHead(req,resp);
        }

        else if("checkNick".equals(actionName)){
            checkNick(req,resp);
        }else if("updateUser".equals(actionName)){
            updateUser(req,resp);
        } else if("regist".equals(actionName)){
            regist(req, resp);
        }else if("checkName".equals(actionName)){
            checkName(req,resp);
        }else if("hospital".equals(actionName)){
            goHospital(req,resp);
        }

    }

    private void goHospital(HttpServletRequest req, HttpServletResponse resp) {
        req.setAttribute("menupage","hospital");

        req.setAttribute("changePage","hospital/hospital.html");
        try {
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Result<User> result =userService.updateUser(req);
    req.setAttribute("result",result);
    req.getRequestDispatcher("user?actionName=userCenter").forward(req,resp);

    }

    private void checkNick(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String nick =req.getParameter("nick");
    User user =(User) req.getSession().getAttribute("user");
    Integer code = userService.checkNick(nick,user.getUserId());
    resp.getWriter().write(code+"");
    resp.getWriter().close();

    }

    private void userHead(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String head=req.getParameter("imageName");
        String realPath=req.getServletContext().getRealPath("/sources/images/");
        File file=new File(realPath+"/"+head);
        String pic=head.substring(head.lastIndexOf(".")+1);
        if("PNG".equalsIgnoreCase(pic)){
            resp.setContentType("image/png");
        }else if("JPG".equalsIgnoreCase(pic)||"JPEG".equalsIgnoreCase(pic)){
            resp.setContentType("image/jpeg");
        }else if("GIF".equalsIgnoreCase(pic)){
            resp.setContentType("image/GIF");
        }

        FileUtils.copyFile(file,resp.getOutputStream());

    }






    private void userCenter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user =(User) req.getSession().getAttribute("user");
        Health health =healthService.findHealth(user.getUserId());

//        Health health = (Health)req.getSession().getAttribute("health");
        if(health!=null){ req.setAttribute("health",health);
        Integer weight=health.getWeight();
            Integer height=health.getHeight();
            Float bmi = weight.floatValue()/height.floatValue()/height.floatValue()*10000;
            String bmix=null;
            if(bmi<18.5){
                bmix="过轻";
            }else if(bmi<24){
                bmix="正常";
            }else{
                bmix="超重";
            }
            Integer xue1=0;
            xue1=health.getXue1();
            Integer xue2=0;
            xue2=health.getXue2();
            Integer xue3=0;
            xue3=health.getXue3();
            Integer xueTotal=4;
            if(xue1!=null&&xue2!=null&&xue3!=null){
            xueTotal=xue1+xue2+xue3;}
            Integer xin1=0;
            xin1=health.getXin1();
            Integer xin2=0;
            xin2=health.getXin2();
            Integer xin3=0;
            xin3=health.getXin3();
            Integer xinTotal=4;
            if(xin1!=null&&xin2!=null&&xin3!=null){
            xinTotal=xin1+xin2+xin3;}
            Integer yi1=0;
            yi1=health.getYi1();
            Integer yi2=0;
            yi2=health.getYi2();
            Integer yi3=0;
            yi3=health.getYi3();
            Integer yiTotal=4;
            if(yi1!=null&&yi2!=null&&yi3!=null){
            yiTotal=yi1+yi2+yi3;}
            Integer jiao1=0;
            jiao1=health.getJiao1();
            Integer jiao2=0;
            jiao2=health.getJiao2();
            Integer jiaoTotal=4;
            if(jiao1!=null&&jiao2!=null){
            jiaoTotal=jiao1+jiao2;}

            req.getSession().setAttribute("bmi",bmi);
            req.getSession().setAttribute("bmix",bmix);
            req.getSession().setAttribute("xueTotal",xueTotal);
            req.getSession().setAttribute("xinTotal",xinTotal);
            req.getSession().setAttribute("yiTotal",yiTotal);
            req.getSession().setAttribute("jiaoTotal",jiaoTotal);
        }


        //设置首页导航高亮
        req.setAttribute("menupage","userCenter");

        req.setAttribute("changePage","user/info.jsp");
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    private void userLogout(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        req.getSession().invalidate();
        Cookie cookie = new Cookie("user",null);
        cookie.setMaxAge(0);
        resp.addCookie(cookie);
        resp.sendRedirect("login.jsp");
    }

    private void userLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName =req.getParameter("userName");
        String userPwd =req.getParameter("userPwd");
        Result<User> result =userService.userLogin(userName,userPwd);
if(result.getCode()==1){
req.getSession().setAttribute("user",result.getResult());
String rem =req.getParameter("rem");
if("1".equals(rem)){
    Cookie cookie1 =new Cookie("users",userName+"-"+userPwd);
    cookie1.setMaxAge(7*24*3600);//一周
    resp.addCookie(cookie1);

}
else{
Cookie cookie =new Cookie("user",null);
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
resp.sendRedirect("question?actionName=view");
}
else{
    req.setAttribute("result",result);
    req.getRequestDispatcher("login.jsp").forward(req,resp);

}

    }
    private void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result<User> result =userService.regist(req);
        req.setAttribute("result",result);
        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String username=req.getParameter("uname");

        Integer code=userService.checkName(username);

        resp.getWriter().write(code+"");
        resp.getWriter().close();
        //req.getRequestDispatcher("login.jsp").forward(req,resp);
    }



}
